/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 */
@Stateless
@LocalBean
public class PushMessageBean {

    private String apiKey = null;
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @PostConstruct
    private void init() {
        apiKey = "AIzaSyD6NTrCFYWm2Tcy2qqw6NjHXDVitM2vkFc";
    }

    public void pushMessage(String[] gcmKey, String msgCode, String message) throws IOException {

        if (gcmKey == null) {
            return;
        }
        ArrayList<String> keyList = new ArrayList<String>();

        for (String gcmKeyStr : gcmKey) {
            keyList.add(gcmKeyStr);
        }
        this.pushMessage(keyList, msgCode, message);
    }

    public void pushMessage(List<String> gcmKey, String msgCode, String message) throws IOException {
        
        if(gcmKey==null)
            return;
        
        JSONArray jsonArr = new JSONArray();

        for (String gcmKeyStr : gcmKey) {
            jsonArr.put(gcmKeyStr);
        }

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("registration_ids", jsonArr);
        
        JSONObject jsonData = new JSONObject();
        jsonData.put("code", msgCode);
        jsonData.put("message", new JSONObject(message));
        
        jsonObj.put("data", jsonData);
        
        System.out.println(jsonObj.toString());
        this.sendMessage(jsonObj.toString());
    }

    private void sendMessage(String message) throws IOException {
        URL url = new URL("https://android.googleapis.com/gcm/send");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setUseCaches(false);
        conn.addRequestProperty("Authorization", "key=" + apiKey);
        conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
      
        conn.connect();
        
        PrintWriter writer = new PrintWriter(conn.getOutputStream());
      
        writer.print(message);
        
        System.out.println(" GCM Message send : " + message);
        
        writer.flush();
      

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        
        String msg = "";

        while (true) {
            String temp = reader.readLine();
          
            if (temp == null) {
                break;
            }
            msg += temp;
        }
        
        System.out.println(msg);
    }
}
